class PageController < ApplicationController

    before_filter :account_is_logged_in, :only => [:page_update, :page_create, :page_destroy]
    layout "site"
    
    # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
    verify :method => :post, :only => [ :page_destroy, :page_create, :page_update ],
    :redirect_to => { :action => :page_list }
    
   
    def page_new
        @include_rte = true
        @newpage = Page.new	
    end   
    
    def page_edit
        @include_rte = true
        @page = @site.get_page(params[:page_title])
        @newpage = @site.get_page(params[:page_title])
    end
    
    def page_create
        @newpage = Page.new(params[:newpage])
        @newpage.site = @site
        
        if @newpage.save
            flash[:notice] = 'Page was successfully created.'
            redirect_to :controller => 'site', :action => 'site_show', :site_directory => @site, :page_title => @newpage 
        else
       	    @include_rte = true
            render :action => 'page_new'
        end
    end
    
    def page_update
        @page = @site.get_page(params[:page_title])
        if @page.update_attributes(params[:newpage])
            flash[:notice] = 'Page was successfully updated.'
            redirect_to :controller => 'site', :action => 'site_show', :site_directory => @site, :page_title => @page
        else
            render :action => 'page_edit'
        end
    end
    
    def page_destroy
        Page.find(params[:id]).destroy
        redirect_to :controller => 'site', :action => 'site_show', :site_directory => @site
    end
end
